#include "BrickEx.h"
#include "Resource.h"


BrickEx::BrickEx(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, int ID, int Obj_state) : Object(x, y)
{
	this->setIDObjType(ID);
	p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\brick_32.png"), 125, 0);
	p_sprite->setFrame(0,0);

	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();

	veloc_x = 0;
	veloc_y = 0;


	_xOld = x;
	_yOld = y;


	accel_y = 0;
	isBreaked = false;

	isLastMoveUp = false;

	if (this->getIDObjType() == 21)			//brick
	{
	}
	else if (this->getIDObjType() == 27)	//brick 5 coin
	{
	}
	_spriteArr[0] = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\exp_brick.png"), 125, 0);
	_spriteArr[1] = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\exp_brick.png"), 125, 0);
	_spriteArr[2] = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\exp_brick.png"), 125, 0);
	_spriteArr[3] = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\exp_brick.png"), 125, 0);

	this->obj_state = Obj_state;
	if (this->obj_state == BS_NONE)
	{
		p_sprite->setFrame(1, 1);
		isLastMoveUp = true;
	}

}

BrickEx::~BrickEx()
{
	/*if (_sprite != NULL)
	{
	delete _sprite;
	}*/
}


void BrickEx::render(int cameraX, int cameraY)
{
	switch(obj_state)
	{
	case BS_NONE:
	case BS_NORMAL:
	case BS_MOVEUP:
		p_sprite->render(pos_x, pos_y, cameraX, cameraY);
		break;
	case BS_BREAKING:
		_spriteArr[0]->render(xBreak1, yBreak1, cameraX, cameraY); 
		_spriteArr[1]->render(xBreak2, yBreak2, cameraX, cameraY);
		_spriteArr[2]->render(2 * pos_x - xBreak1, yBreak1, cameraX, cameraY);
		_spriteArr[3]->render(2 * pos_x - xBreak2, yBreak2, cameraX, cameraY);
		break;
	}
}


void BrickEx::update(list<Object*>* obj_list, DxInput* input, float gameTime)
{
	list<Object*>::iterator i;
	Object* obj;

	switch(obj_state)
	{
	case BS_NONE:

		p_sprite->update(gameTime);
		break;

	case BS_NORMAL:

		p_sprite->update(gameTime);
		break;
	case BS_MOVEUP:


		veloc_y -= accel_y * gameTime;
		pos_y += veloc_y * gameTime + 1.0f/2.0f * accel_y * gameTime * gameTime;

		//Xu li dung gach voi coin,...
		for (i = obj_list->begin(); i != obj_list->end(); i++)
		{
			obj = *i;

			Box box(this->getPosX() + width/2,
				this->getPosY() - this->getHeight(), 
				this->getWidth()/2, 
				this->getHeight(), 
				0, 
				veloc_y * gameTime + 1.0f/2.0f * accel_y * gameTime * gameTime);			
			Box block(obj->getPosX() + obj->getWidth()/2,
				obj->getPosY() - obj->getHeight(),// - 0.11,
				obj->getWidth()/2,
				obj->getHeight()
				);

			Box boxphase = Collision::GetSweptBroadphaseBox(box);
			if (Collision::AABBCheck(boxphase, block))
			{
				switch (obj->getIDObjType())
				{
					//case 41:	//coin
					//case 46:	//coin an
					//case 47:	//5 coin hidden
					//	if (obj->getObjState() == IS_IDLE)
					//	{
					//		obj->changeState(IS_ACTIVING);
					//		obj->setIsChangeState(true);
					//		if (obj->getNumCoinZero() == true)
					//		{
					//			isLastMoveUp = true;
					//			changeState(BS_NONE);
					//		}
					//		
					//		
					//	}
					//	choi an gian nhe
					//	break;
					//case 43:	//nam mang
					//	if (obj->getObjState() == IS_IDLE)
					//	{
					//		obj->changeState(IS_ACTIVING);
					//	}
					//	break;
					//case 45:	//nam to, hoa
					//	if (obj->getObjState() == IS_IDLE && obj->isActiveMushRoom == false)
					//	{
					//		obj->isActiveMushRoom = true;
					//		//obj->changeState(IS_ACTIVING);
					//	}
					//	break;

					//case 44:	//sao
					//	if (obj->getObjState() == IS_IDLE)
					//	{
					//		obj->changeState(IS_ACTIVING);
					//	}
					//	break;

				case 11:		//goomba
				case 12:		//koopba
					obj->changeState(ES_FALL);
					break;


				}

			}
		}

		if (pos_y <= _yOld)
		{
			changeState(BS_NORMAL);
		}





		p_sprite->update(gameTime);

		break;
	case BS_BREAKING:
		xBreak1 += vxBreak1 * gameTime;
		xBreak2 += vxBreak2 * gameTime;

		vyBreak1 -= ayBreak * gameTime;
		vyBreak2 -= ayBreak * gameTime;

		yBreak1 += vyBreak1 * gameTime + 1.0f/2.0f * ayBreak * gameTime * gameTime;
		yBreak2 += vyBreak2 * gameTime + 1.0f/2.0f * ayBreak * gameTime * gameTime;


		//Xu li dung gach voi coin,...
		for (i = obj_list->begin(); i != obj_list->end(); i++)
		{
			obj = *i;

			Box box(this->getPosX() + width/2,
				this->getPosY() - this->getHeight(), 
				this->getWidth()/2, 
				this->getHeight(), 
				0, 
				vyBreak1 * gameTime + 1.0f/2.0f * ayBreak * gameTime * gameTime);


			Box block(obj->getPosX() + obj->getWidth()/2,
				obj->getPosY() - obj->getHeight(),// - 0.11,
				obj->getWidth()/2,
				obj->getHeight()
				);

			Box boxphase = Collision::GetSweptBroadphaseBox(box);
			if (Collision::AABBCheck(boxphase, block))
			{
				switch (obj->getIDObjType())
				{
				case 11:		//goomba
				case 12:		//koopba
					obj->changeState(ES_FALL);
					break;					
				}

			}
		}


		if(yBreak1 <= 0)
		{
			changeState(BS_REMOVE);
		}
		_spriteArr[0]->update(gameTime);
		_spriteArr[1]->update(gameTime);
		_spriteArr[2]->update(gameTime);
		_spriteArr[3]->update(gameTime);
		
		break;
	}
}

void BrickEx::changeState(int state)
{
	obj_state = state;
	switch (obj_state)
	{
	case BS_NORMAL:

		isChangeState = true;

		pos_y = _yOld;
		//_vy = 0;
		veloc_y = 0;

		accel_y = 0;
		break;
	case BS_MOVEUP:

		isChangeState = true;

		veloc_y = 1.0;
		//_accelY = GRAVITY;
		accel_y = GRAVITY;
		break;
	case BS_BREAKING:
		xBreak1 = xBreak2 = pos_x + 40/2;
		yBreak1 = pos_y;
		yBreak2 = pos_y + 40;

		vyBreak1 = 1.0;
		vyBreak2 = 0.8;

		vxBreak1 = 0.150;
		vxBreak2 = 0.100;
		ayBreak = GRAVITY;
		break;
	case BS_NONE:
		p_sprite->setFrame(1, 1);
		isLastMoveUp = true;
		pos_y = _yOld;
		veloc_y = 0;
		accel_y = 0;
		break;
	case BS_REMOVE:
		vxBreak1 = vxBreak2 = 0;
		vyBreak1 = vyBreak2 = 0;
		ayBreak = 0;
		break;
	}
}

OBJECT_TYPE BrickEx::getObjectType()
{
	return OBJECT_LAND;
}